package com.ys.controller.api.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.base.TpAmazonRegion;
import com.ys.entity.sys.TsFilesInfo;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tsFilesInfo")
public class ApiInterTsFilesInfoController {

	@Autowired
	private CommonApiService<TsFilesInfo> commonApiService;

	@RequestMapping("/searchForC88")
    @ResponseBody
	public LayuiTableVoJA searchForC88(@ModelAttribute TsFilesInfo cond){
		cond = setSeachSql(cond);


		cond.setOrderby("A.Id DESC");

		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/searchForModal")
    @ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TsFilesInfo cond){

		cond.setVtype("C88");

		cond.addLeftJoin("TP_FbaLogsticsOrder B ON B.Bid = A.BidLogsticsOrder")
			.addField("B.CodeLogsticsServer codeLogsticsServer")
			.addField("B.EntryNumber entryNumber")

			.addLeftJoin("TP_AmazonShop D ON D.Guid = B.SellerId")
			.addLeftJoin("TP_AmazonSeller C ON C.id = D.amazonSellerId")
			.addField("C.Vgsbm vgsbm")
			.addField("C.Vgsjc vgsjc")

			.addLeftJoin("THR_OperateCompanyApply E ON E.Vgsbm = C.Vgsbm")
			.addLeftJoin("TS_UserChange F ON F.Id = E.OperatorChangeId")
			.addField("F.UserId operatorUserId");

		cond.where("A.Id not in (select CbbId from THR_VatApplyDetailCbb)");

    	cond = commonApiService.dataAuth(MenuId.TCC_LogisticsSadFile, cond);

		return commonApiService.searchLay(cond);
	}

	/*
	 * 导出
	 */
	@RequiresPermissions("tsFilesInfo:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TsFilesInfo cond) throws Exception{
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");

		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();

    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TCC_LogisticsSadFile)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();


        ExportExcelUtils<TpAmazonRegion> util = new ExportExcelUtils<TpAmazonRegion>(new TpAmazonRegion());

        String toFilePath = util.exportExcel(listData, listField, "C88文件管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     */
    public TsFilesInfo setSeachSql(TsFilesInfo cond){
		cond.setVtype("C88");

		cond.addLeftJoin("TP_FbaLogsticsOrder B ON B.Bid = A.BidLogsticsOrder")
			.addField("B.CodeLogsticsServer codeLogsticsServer")
			.addField("B.EntryNumber entryNumber")

			.addLeftJoin("TP_AmazonShop D ON D.Guid = B.SellerId")
			.addLeftJoin("TP_AmazonSeller C ON C.id = D.amazonSellerId")
			.addField("C.Vgsbm vgsbm")
			.addField("C.Vgsjc vgsjc")

			.addLeftJoin("THR_OperateCompanyApply E ON E.Vgsbm = C.Vgsbm")
			.addLeftJoin("TS_UserChange F ON F.Id = E.OperatorChangeId")
			.addField("F.UserId operatorUserId");

    	cond = commonApiService.dataAuth(MenuId.TCC_LogisticsSadFile, cond);

        return  cond;
    }
}
